package com.fattyCorps.common.obj.config;

import cn.hutool.extra.spring.SpringUtil;
import com.fattyCorps.common.mgr.Cfg;
import com.fattyCorps.common.obj.msg.server.battle.DHeroShort;
import com.fattyCorps.common.packet.dto.DItem;
import com.fattyCorps.common.util.CfgUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class MainGuanqianMould extends BaseCfg {
    private String name;
    private Integer chapter;
    private Integer no;
    private Integer unlockLv;
    private String addCoin;
    private String addExp;
    private String baseAwards;
    private String hangAwards;
    private String enemies;

    private final String SPLIT = ",";

    private class DExp {
        protected int freq;
        protected int add;
    }

    private class DCoin extends DExp {
    }

    private class DAward {
        public int typeId;
        public int duration;
        public int num;
    }

    List<DAward> awardsFormat = null;

    // 过关奖励
    public List<DItem> getBaseAwards() {
        return CfgUtils.cfgStrToBeanList(baseAwards, DItem.class);
    }

    public List<DItem> getHangAwards(int duration) {
        // itemTypeId, duration, num
        String[] split = hangAwards.split(SPLIT);

        if (awardsFormat == null) {
            awardsFormat = new ArrayList<>();
            DAward d = null;
            final int DAWARD_STRUCT_COUNT = 3;

            for (int i = 0; i < split.length; i++) {
                if (i % DAWARD_STRUCT_COUNT == 0) {
                    d = new DAward();
                    d.typeId = Integer.valueOf(split[i]);
                } else if (i % DAWARD_STRUCT_COUNT == 1) {
                    d.duration = Integer.valueOf(split[i]);
                } else {
                    d.num = Integer.valueOf(split[i]);
                    awardsFormat.add(d);
                }
            }
        }

        List<DItem> awards = new ArrayList<>();
        for (DAward dAward : awardsFormat) {
            int num = duration / dAward.duration;
            if (num <= 0) {
                continue;
            }

            DItem dItem = new DItem();
            dItem.setTypeId(dAward.typeId);
            dItem.setNum(num * dAward.num);
            awards.add(dItem);
        }

        return awards;
    }

    public int getAddCoin(int duration) {
        String[] split = addCoin.split(SPLIT);
        int freq = Integer.valueOf(split[0]);
        int add = Integer.valueOf(split[1]);
        int amount = duration / freq * add;
        return amount;
    }

    public int getAddExp(int duration) {
        String[] split = addExp.split(SPLIT);
        int freq = Integer.valueOf(split[0]);
        int add = Integer.valueOf(split[1]);
        int amount = duration / freq * add;
        return amount;
    }

    public List<DHeroShort> getEnemies() {
        // standpos, typeId, lv, star
        return CfgUtils.cfgStrToBeanList(enemies, DHeroShort.class);
    }

    public static int maxGuanqia() {
        return SpringUtil.getBean(Cfg.class).getCfgAll(MainGuanqianMould.class).size();
    }
}
